{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python基础教学代码---进阶篇  \n",
    "1.循环  \n",
    "2.条件语句:if   \n",
    "3.文件I/O  \n",
    "4.异常处理  \n",
    "5.导包\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.循环  \n",
    "两种循环：  \n",
    "1.1for 循环  \n",
    "1.2while  循环  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for 循环打印List（列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1种循环取值方式:直接取值\n",
      "Chile\n",
      "b\n",
      "c\n",
      "--------------------------\n",
      "第2种循环取值方式:索引取值\n",
      "Chile\n",
      "b\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "str_list = ['Chile', 'b', 'c']\n",
    "\n",
    "print('第1种循环取值方式:直接取值')\n",
    "for sub_str in str_list:\n",
    "    print(sub_str)\n",
    "\n",
    "print('--------------------------')\n",
    "print('第2种循环取值方式:索引取值')   \n",
    "for i in range(len(str_list)):\n",
    "    print(str_list[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while 循环打印List（列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chile\n",
      "b\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "str_list = ['Chile', 'b', 'c']\n",
    "i = 0\n",
    "while i < len(str_list):\n",
    "    print(str_list[i])\n",
    "    i += 1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for 循环打印Tuple（元组）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1种循环取值方式:直接取值\n",
      "chile\n",
      "111\n",
      "2.2\n",
      "a\n",
      "['Chile', 'b', 'c']\n",
      "--------------------------\n",
      "第2种循环取值方式:索引取值\n",
      "chile\n",
      "111\n",
      "2.2\n",
      "a\n",
      "['Chile', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "str_list = ['Chile', 'b', 'c']\n",
    "mix_tuple = ('chile', 111, 2.2, 'a', str_list)  # 不可赋值\n",
    "print('第1种循环取值方式:直接取值')\n",
    "for sub_tuple in mix_tuple:\n",
    "    print(sub_tuple)\n",
    "\n",
    "print('--------------------------')\n",
    "print('第2种循环取值方式:索引取值')   \n",
    "for i in range(len(mix_tuple)):\n",
    "    print(mix_tuple[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### while 循环打印Tuple（元组）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "chile\n",
      "111\n",
      "2.2\n",
      "a\n",
      "['Chile', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "str_list = ['Chile', 'b', 'c']\n",
    "mix_tuple = ('chile', 111, 2.2, 'a', str_list)  # 不可赋值\n",
    "i = 0\n",
    "while i < len(mix_tuple):\n",
    "    print(mix_tuple[i])\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for 循环打印Dictionary（字典）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "key: name\n",
      "value: Chile\n",
      "-------------\n",
      "key: age\n",
      "value: 18\n",
      "-------------\n",
      "key: num_list\n",
      "value: [1, 2, 3, 4]\n",
      "-------------\n",
      "key: tuple\n",
      "value: ('chile', 111, 2.2, 'a', ['Chile', 'b', 'c'])\n",
      "-------------\n"
     ]
    }
   ],
   "source": [
    "str_list = ['Chile', 'b', 'c']\n",
    "mix_tuple = ('chile', 111, 2.2, 'a', str_list)  # 不可赋值\n",
    "num_list = [1, 2, 3, 4]\n",
    "test_dict = {'name': 'Chile', 'age': 18, 'num_list': num_list, 'tuple': mix_tuple}\n",
    "for key in test_dict.keys():  # 键值对打印法\n",
    "    print('key:', key)\n",
    "    print('value:', test_dict[key])\n",
    "    print('-------------')\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for 循环打印Set（集合）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abc   <class 'str'>\n",
      "1   <class 'int'>\n",
      "1   <class 'str'>\n",
      "chile   <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "test_set = {'abc', 1, 1, '1', 'chile'}  \n",
    "for value in test_set:\n",
    "    print(value, ' ', type(value))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.条件语句:if\n",
    "2.1 == : 恒等符号  \n",
    "2.2 != : 不等符号    \n",
    "2.3 > :大于号  \n",
    "2.4 < :小于号  \n",
    "2.5 >=:大于等于号  \n",
    "2.6 <=:小于等于号    \n",
    "2.7 and:与   \n",
    "2.8 or:或  \n",
    "2.9 not:非  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a != b\n"
     ]
    }
   ],
   "source": [
    "a = 1  # 数字\n",
    "b = '1'  # 字符串\n",
    "if a == b:\n",
    "    print('a == b')\n",
    "else:\n",
    "    print('a != b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a < b\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "b = 2\n",
    "if a > b:\n",
    "    print('a > b')\n",
    "elif a < b:\n",
    "    print('a < b')\n",
    "else:\n",
    "    print('a == b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = True\n",
    "b = False\n",
    "if a and b:\n",
    "    print('True')\n",
    "else:\n",
    "    print('False')\n",
    "    \n",
    "if a or b:\n",
    "    print('True')\n",
    "else:\n",
    "    print('False')\n",
    "\n",
    "if a and (not b):\n",
    "    print('True')\n",
    "else:\n",
    "    print('False')\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.文件I/O"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "权限：  \n",
    "w: 写权限  \n",
    "r：读权限  \n",
    "a：在原有文本的基础上追加文本的权限  \n",
    "\n",
    "互联网上的文件有非常多的格式，这里只是举个例子让大家感受下。至于更多格式的读写，  \n",
    "小伙伴们可以通过互联网去搜索，Python是兼容很多文件格式的读写，代码风格都差不多。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('text.txt', 'w') as fw:  #  只有文件名，默认文件在统计目录\n",
    "    string = 'I am chile!'\n",
    "    for i in range(5):\n",
    "        fw.write(string + '\\n')    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('text.txt', 'r') as fr:\n",
    "    for line in fr:\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('text.txt', 'a') as fw:\n",
    "    string = 'You are handsome!'\n",
    "    for i in range(5):\n",
    "        fw.write(string + '\\n')    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "I am chile!\n",
      "\n",
      "You are handsome!\n",
      "\n",
      "You are handsome!\n",
      "\n",
      "You are handsome!\n",
      "\n",
      "You are handsome!\n",
      "\n",
      "You are handsome!\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('text.txt', 'r') as fr:\n",
    "    for line in fr:\n",
    "        print(line) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.异常\n",
    "\n",
    "try:    \n",
    "    执行正常代码。   \n",
    "    \n",
    "except:   \n",
    "    发生异常，执行此处代码    \n",
    "    \n",
    "else:（这段代码可不加）   \n",
    "    无异常，则执行此处代码  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The file does not exist!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    with open('txr.txt', 'r') as fr:\n",
    "        text = fr.read()\n",
    "except IOError:\n",
    "    print('The file does not exist!')\n",
    "\n",
    "else:\n",
    "    print('Succeed')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 异常处理的目的  \n",
    "小伙伴们一开始写Python的时候可能并不需要的异常处理机制，因为我们的代码简洁又高效，不过这并不代表你永远不需要。  \n",
    "现代软件是非常庞大的，而代码又是人写的，难免会出错，你不知道一个大型软件在运行过程中会在什么时候出现一个bug，这时候  \n",
    "异常处理机制就能让你快速定位自己软件的bug，缩短我们调试的时间，这就是异常处理机制的用途。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.导包  \n",
    "1.本地包  \n",
    "2.系统包  \n",
    "  \n",
    "导包指令：  \n",
    "1.import    \n",
    "2.from ... import ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入本地包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello!\n"
     ]
    }
   ],
   "source": [
    "from test import hello\n",
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello!\n"
     ]
    }
   ],
   "source": [
    "import test\n",
    "test.hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入系统包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-08-21 09:43:07\n"
     ]
    }
   ],
   "source": [
    "import time # 引入时间模块\n",
    "# 格式化成year-month-day hour:min:sec形式\n",
    "print (time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime())) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导包的目的  \n",
    "Python之所以被称为胶水语言，是因为它有很多优秀的第三方包，让我们在编程过程中只关注任务的解决，而不拘泥于代码的繁琐，  \n",
    "提升代码的复用率，加快编程速度。因此，导包是Python不可或缺的重要技能。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
